package cn.signalpha.modules.schedule.controller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.schedule.entity.ScheduleJob;
import cn.signalpha.modules.schedule.entity.ScheduleJobLog;
import cn.signalpha.modules.schedule.service.ScheduleJobLogService;
import cn.signalpha.modules.schedule.service.ScheduleJobService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 定时任务
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/admin/schedule")
@Api(value = "定时任务", tags = "定时任务")
public class ScheduleJobController {

	private final ScheduleJobService scheduleJobService;
	private final ScheduleJobLogService scheduleJobLogService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<ScheduleJob>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query){
		return R.data(scheduleJobService.page(Condition.getPage(query), Condition.getQueryWrapper(params, ScheduleJob.class)));
	}

	@GetMapping("/log/page")
	@ApiOperation(value = "日志分页", notes = "")
	public R<IPage<ScheduleJobLog>> logPage(@RequestParam Long jobId, Query query){
		return R.data(scheduleJobLogService.page(Condition.getPage(query), Wrappers.<ScheduleJobLog>lambdaQuery().eq(ScheduleJobLog::getJobId, jobId).orderByDesc(ScheduleJobLog::getLogId)));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<ScheduleJob> info(@RequestParam Long jobId){
		return R.data(scheduleJobService.getById(jobId));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入scheduleJob")
	public R save(@Valid @RequestBody ScheduleJob scheduleJob){
		return R.status(scheduleJobService.saveJob(scheduleJob));
	}

	@PostMapping("/update")
	@ApiOperation(value = "更新", notes = "传入scheduleJob")
	public R update(@Valid @RequestBody ScheduleJob scheduleJob){
		return R.status(scheduleJobService.update(scheduleJob));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入jobIds")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids){
		return R.status(scheduleJobService.deleteBatch(Func.toIntArray(ids)));
	}

	@PostMapping("/run")
	@ApiOperation(value = "立即执行", notes = "传入jobIds")
	public R run(@ApiParam(value = "主键集合", required = true) @RequestParam String ids){
		scheduleJobService.run(Func.toIntArray(ids));
		return R.status(true);
	}

	@PostMapping("/pause")
	@ApiOperation(value = "暂停运行", notes = "传入jobIds")
	public R pause(@ApiParam(value = "主键集合", required = true) @RequestParam String ids){
		scheduleJobService.pause(Func.toIntArray(ids));
		return R.status(true);
	}

	@PostMapping("/resume")
	@ApiOperation(value = "恢复运行", notes = "传入ids")
	public R resume(@ApiParam(value = "主键集合", required = true) @RequestParam String ids){
		scheduleJobService.resume(Func.toIntArray(ids));
		return R.status(true);
	}

}
